package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserMarriage;
import com.lantosec.hmrp.modules.archives.mapper.UserMarriageMapper;
import com.lantosec.hmrp.modules.archives.service.IUserMarriageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserMarriageVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 领导干部婚姻变化情况报告表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-12
 */
@Service
@Primary
@Log4j2
public class UserMarriageServiceImpl extends ServiceImpl<UserMarriageMapper, UserMarriage> implements IUserMarriageService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Override
    public void saveUserMarriage(UserMarriageVO vo) {
        UserMarriage userMarriage = vo.convertToEntity();
        userMarriage.setUserMarriageId(UUIDUtils.get());
        //获取当前用户id和名称
        userMarriage.setCreateId(TokenUtil.getUserId());
        userMarriage.setCreateUser(TokenUtil.getName());
        userMarriage.setCreateTime(DateUtils.nowDate());
        userMarriage.setVerifyStatus(1);
        this.save(userMarriage);
    }

    @Override
    public void updateUserMarriage(UserMarriageVO vo) {
        String userMarriageId = vo.getUserMarriageId();
        //新增一条数据
        //saveUserMarriage(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userMarriageId)) {
            UserMarriage userMarriage = vo.convertToEntity();
            userMarriage.setVerifyStatus(1);
            this.updateById(userMarriage);
        }else {
            saveUserMarriage(vo);
        }
    }

    @Override
    public List<UserMarriageVO> getUserMarriage(String userId) {
        List<UserMarriageVO> userMarriageVOList = new ArrayList<>();
        List<UserMarriage> userMarriageList = this.list(new QueryWrapper<UserMarriage>().eq(UserMarriage.ColName.user_id.toString(),userId).orderByDesc(UserMarriage.ColName.create_time.toString()));
        userMarriageList.forEach(param->{
            UserMarriageVO userMarriageVO = getUserMarriage(param);
            userMarriageVOList.add(userMarriageVO);
        });
        return userMarriageVOList;
    }

    /**
     * 组装领导干部婚姻变化情况
     * @param userMarriage
     */
    private UserMarriageVO getUserMarriage(UserMarriage userMarriage) {
        UserMarriageVO userMarriageVO = userMarriage.convertToVO();
        try {
            //查询字典表对应的配偶政治面貌名称
            String matePoliticalStatus = userMarriage.getMatePoliticalStatus();
            String matePoliticalName = iSysDictionariesService.getById(matePoliticalStatus).getDictionariesShowName();
            userMarriageVO.setMatePoliticalName(matePoliticalName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userMarriageVO;
    }

    @Override
    public List<UserMarriageVO> getUserMarriageVerify(String userId) {
        List<UserMarriageVO> userMarriageVOList = new ArrayList<>();
        List<UserMarriage> userMarriageList = this.list(new QueryWrapper<UserMarriage>().eq(UserMarriage.ColName.user_id.toString(),userId).in(UserMarriage.ColName.verify_status.toString(),0,2));
        userMarriageList.forEach(param->{
            UserMarriageVO userMarriageVO = getUserMarriage(param);
            userMarriageVOList.add(userMarriageVO);
        });
        return userMarriageVOList;
    }

    @Override
    public void verifyUserMarriage(UserMarriageVO vo) {
        UserMarriage userMarriage = vo.convertToEntity();
        Integer verifyStatus = userMarriage.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userMarriage.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userMarriage.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userMarriage.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userMarriage.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userMarriage.setVerifyId(TokenUtil.getUserId());
        userMarriage.setVerifyUser(TokenUtil.getName());
        userMarriage.setVerifyTime(DateUtils.nowDate());
        this.updateById(userMarriage);
    }

    @Override
    public List<UserMarriageVO> getUserMarriageHistory(String userId) {
        List<UserMarriageVO> userMarriageVOList = new ArrayList<>();
        List<UserMarriage> userMarriageList = this.list(new QueryWrapper<UserMarriage>().eq(UserMarriage.ColName.user_id.toString(),userId).eq(UserMarriage.ColName.verify_status.toString(),4));
        userMarriageList.forEach(param->{
            UserMarriageVO userMarriageVO = getUserMarriage(param);
            userMarriageVOList.add(userMarriageVO);
        });
        return userMarriageVOList;
    }
}
